package com.mobi.Survey.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mobi.Survey.controller.Pojo;
import com.mobi.Survey.controller.SubscriptionPojo;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SubscriptionTable.java Table of Subscription data of Survey
 * 
 */
public class SubscriptionTable extends AbstractTable
{

	public static final String TABLE = "subscription";

	public static final String COL_ID = "_id";
	public static final String COL_SUBS_ID = "subs_id";
	public static final String COL_SUBS_FNAME = "fname";
	public static final String COL_SUBS_LNAME = "lname";
	public static final String COL_SUBS_EMAIL = "email";
	public static final String COL_SUBS_STREET_NUMBER = "StreetNumber";
	public static final String COL_SUBS_STREET_NAME = "StreetName";
	public static final String COL_SUBS_CITY = "City";
	public static final String COL_SUBS_STATE = "State";
	public static final String COL_SUBS_COUNTRY = "Country";
	public static final String COL_SUBS_ZIP = "Zip";

	public SubscriptionTable(Context ctx)
	{
		super(ctx);
	}

	/**
	 * Single subscription data could belongs to many Surveys so when entering
	 * each survey we check weather we already have a subscription record of
	 * that survey or not
	 * 
	 * @param subsId
	 *            Id of Survey against which check
	 * @return true if exist
	 */
	public boolean isNewSubscription(long subsId)
	{
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		boolean is = true;
		Cursor cursor = db.query(TABLE, new String[] { COL_SUBS_ID },
				COL_SUBS_ID + "=? ", new String[] { subsId + "" }, null, null,
				null);
		if (cursor != null)
		{

			if (cursor.moveToFirst() && cursor.getCount() > 0)
				is = false;
			cursor.close();
			cursor = null;
		}

		return is;

	}

	public SubscriptionTable()
	{

	}

	@Override
	public long insertRow(Pojo obj)
	{

		SubscriptionPojo subscription = (SubscriptionPojo) obj;

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		ContentValues cv = new ContentValues();

		cv.put(COL_SUBS_ID, subscription.getSubsId());
		cv.put(COL_SUBS_FNAME, subscription.getfName());

		cv.put(COL_SUBS_LNAME, subscription.getlName());

		cv.put(COL_SUBS_EMAIL, subscription.getEmail());

		cv.put(COL_SUBS_STREET_NUMBER, subscription.getAddress()
				.getStreetNumber());
		cv.put(COL_SUBS_STREET_NAME, subscription.getAddress().getStreetName());
		cv.put(COL_SUBS_CITY, subscription.getAddress().getCity());
		cv.put(COL_SUBS_STATE, subscription.getAddress().getState());
		cv.put(COL_SUBS_COUNTRY, subscription.getAddress().getCountry());
		cv.put(COL_SUBS_ZIP, subscription.getAddress().getZip());

		long id = db.insert(TABLE, null, cv);
		return id;

	}

	@Override
	public boolean createTable(SQLiteDatabase sqliteDb)
	{
		String query = "create table " + TABLE + "( " + COL_ID
				+ " integer primary key autoincrement, " + COL_SUBS_ID
				+ " BIGINT unique,  " + COL_SUBS_FNAME + " text, "
				+ COL_SUBS_LNAME + " text, " + COL_SUBS_EMAIL + " text, "
				+ COL_SUBS_STREET_NUMBER + " text, " + COL_SUBS_STREET_NAME
				+ " text, " + COL_SUBS_CITY + " text, " + COL_SUBS_STATE
				+ " text, " + COL_SUBS_COUNTRY + " text, " + COL_SUBS_ZIP
				+ " text )";
		sqliteDb.execSQL(query);
		printMessage(query);
		return true;
	}

	public SubscriptionPojo getSubscriberData(int subsId)
	{
		SubscriptionPojo sub = new SubscriptionPojo();
		SQLiteDatabase db = dbHelper.getReadableDatabase();

		Cursor cursor = db.query(TABLE, null, COL_SUBS_ID + " = ? ",
				new String[] { subsId + "" }, null, null, null);

		if (cursor != null)
		{

			if (cursor.moveToFirst())
			{

				do
				{
					sub.setSubsId(cursor.getInt(1));

					sub.setfName(cursor.getString(2));
					sub.setlName(cursor.getString(3));
					sub.setEmail(cursor.getString(4));

					sub.getAddress().setStreetNumber(cursor.getString(5));
					sub.getAddress().setStreetName(cursor.getString(6));
					sub.getAddress().setCity(cursor.getString(7));
					sub.getAddress().setState(cursor.getString(8));
					sub.getAddress().setCountry(cursor.getString(9));
					sub.getAddress().setZip(cursor.getString(10));

				} while (cursor.moveToNext());

			}

			cursor.close();
			cursor = null;
		}

		return sub;
	}

	@Override
	public boolean deleteRow(long id)
	{
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int ret = db.delete(TABLE, COL_SUBS_ID + " = ? ", new String[] { id
				+ "" });
		return ret > 0;
	}

	@Override
	public void deleteAllData()
	{
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE, null, null);

	}

}
